package com.test.webfluxhtml.config;

import com.test.webfluxhtml.filter.AuthFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;
import org.springframework.web.util.pattern.PathPatternParser;
import reactor.ipc.netty.http.server.HttpServerOptions;


@Configuration
public class WebConfig   {

    @Autowired
    Environment environment;

    @Autowired
    NettyHttpServerConfig nettyHttpServerConfig;



//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/upload/**")
//                .allowedOrigins("*")
//                .allowedHeaders("*")
//                .allowedMethods("GET","POST","OPTIONS");
//        registry.addMapping("/get/**") .allowedOrigins("*")
//                .allowedHeaders("*")
//                .allowedMethods("GET","POST","OPTIONS");
//    }

    @Bean
    public WebFilter sampleWebFilter()
    {
        AuthFilter filter = new AuthFilter();
        return filter;
    }

    @Bean
    public ReactiveWebServerFactory NettyReactiveWebServerFactory() {
        NettyReactiveWebServerFactory nettyReactiveWebServerFactory = new NettyReactiveWebServerFactory();
        nettyReactiveWebServerFactory.addServerCustomizers(new NettyServerCustomizer() {
            @Override
            public void customize(HttpServerOptions.Builder builder) {
                builder.maxInitialLineLength(nettyHttpServerConfig.getMaxInitialLineLength());
                builder.maxHeaderSize(nettyHttpServerConfig.getMaxHeaderSize());
            }
        });
        return  nettyReactiveWebServerFactory;
    }

//    @Bean
//    public ReactiveWebServerFactory httpServer() {
//         Consumer<Channel> channelConfigurator = channel -> Optional
//                .ofNullable(channel.pipeline().get(NettyPipeline.ReactiveBridge))
//                .map(NettyPipeline.SendOptions.class::cast)
//                .ifPresent(NettyPipeline.SendOptions::flushOnEach);
//
//        return httpHandler -> {
//            HttpServer httpServer = HttpServer.builder()
//                    .options(serverOptionsBuilder -> serverOptionsBuilder
//                            .afterChannelInit(channelConfigurator)
//                            .port(Integer.parseInt(environment.getProperty("server.port")))
//                            .maxInitialLineLength(nettyHttpServerConfig.getMaxInitialLineLength())
//                            .initialBufferSize(nettyHttpServerConfig.getInitialBufferSize())
//                            .maxChunkSize(nettyHttpServerConfig.getMaxChunkSize())
//                            .maxHeaderSize(nettyHttpServerConfig.getMaxHeaderSize())
//                            .validateHeaders(nettyHttpServerConfig.isValidateHeaders())
//                    )
//                    .build();
//            System.out.println(httpServer.options().httpCodecMaxChunkSize());
//            System.out.println(httpServer.options().httpCodecInitialBufferSize());
//            System.out.println(httpServer.options().httpCodecMaxHeaderSize());
//            System.out.println(httpServer.options().httpCodecMaxInitialLineLength());
//            System.out.println(httpServer.options().httpCodecValidateHeaders());
//            System.out.println(httpServer.options().minCompressionResponseSize());
//            return new NettyWebServer(httpServer, new ReactorHttpHandlerAdapter(httpHandler),Duration.ofMinutes(10));
//        };
//    }


    @Bean
    public CorsWebFilter corsWebFilter()
    {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("GET");
        corsConfiguration.addAllowedMethod("POST");
        corsConfiguration.addAllowedMethod("OPTIONS");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**",corsConfiguration);
        return  new CorsWebFilter(source);
    }
}
